<?php
//近期读者小工具
class cy_readers extends WP_Widget {

	function cy_readers() {
		$widget_ops = array('description' => '显示近期回复最多的读者');
		$this->WP_Widget('cy_readers', '&#xE000; 活跃读者', $widget_ops);
	}

	function form($instance) {
		$title = isset($instance['title']) ? $instance['title'] : '本站贵宾';
		$limit = isset($instance['limit']) ? $instance['limit'] : 10;
		$time = isset($instance['time']) ? $instance['time'] : 30;
		$img_size = isset($instance['img_size']) ? $instance['img_size'] : 47;
?>
	<p>
		<label>标题：
			<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
		</label>
	</p>

	<p>
		<label>访客数量：
			<input id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit; ?>" size="6" />
		</label>
	</p>

	<p>
		<label>统计天数：
			<input id="<?php echo $this->get_field_id('time'); ?>" name="<?php echo $this->get_field_name('time'); ?>" type="text" value="<?php echo $time; ?>" size="6" />
		</label>
	</p>

	<p>
	<label>头像大小：
			<input id="<?php echo $this->get_field_id('img_size'); ?>" name="<?php echo $this->get_field_name('img_size'); ?>" type="text" value="<?php echo $img_size; ?>" size="6" />
		</label>
	</p>
<?php
	}

	function update($new_instance, $old_instance) { return $new_instance; }

	function widget($args, $instance) {
		extract( $args );
		$title = $instance['title'];
		$limit = $instance['limit'];
		$time = $instance['time'];
		$img_size = $instance['img_size'];
		echo $before_widget.$before_title.$title.$after_title;
		echo cy_widget_readers($img_size,$time,$limit);
		echo $after_widget;
	}
}
register_widget('cy_readers');

function cy_widget_readers($img_size, $time, $limit){
?>
<ul class="cy_readers">
<?php
	global $wpdb;
	$sql="SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID) WHERE comment_date > date_sub( NOW(), INTERVAL $time day ) AND user_id='0' AND comment_author_email != '' AND post_password='' AND comment_approved='1' AND comment_type='') AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT $limit";
	$wall = $wpdb->get_results($sql);
	foreach ($wall as $comment) {
		$avatar = get_avatar( $comment->comment_author_email, $img_size );
		if($comment->comment_author == ""){$comment->comment_author="Anonymous";}
		if( $comment->comment_author_url )
			$url = 'href="'.$comment->comment_author_url.'" rel="external nofollow"';
		else $url='';
		$width = $img_size + 12;
		$tmp.= "		<li><a $url title='".$comment->comment_author." 留下".$comment->cnt."条点评' style=width:{$width}px ><img class=\"avatar slower\" src=\"".TPDIR."/res/avatar.jpg\" alt=\"avatar\" data-original=\"".preg_replace(array('/^.+(src=)(\"|\')/i', '/(\"|\')\sclass=(\"|\').+$/i'), array('', ''), $avatar )."\" width={$img_size} height={$img_size} ";
		if(isIE8()) $tmp.= "style=width:{$img_size}px;height:{$img_size}px";
		$tmp.= " /></a></li>";
	}
	echo $tmp;
?>
	<div class="clear"></div>
</ul>
<?php }?>